﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;

using Willson.JsbManager.Idal;
using Willson.JsbManager.Entity;
using Willson.JsbManager.BllEntity;
using Willson.JsbManager.Comm;

namespace Willson.JsbManager.Bll
{
    /// <summary>
    /// 单位月用水信息录入
    /// </summary>
    public class BLLSbMonthInput:BaseBLL
    {
        private ITableXtCompany xt_company_DAL = null;
        private ITableXtCompany_sb_info xt_company_sb_info_DAL = null;
        private ITableXtSbMonth xt_sb_month_DAL = null;

        public BLLSbMonthInput()
        {
            xt_company_DAL = Reflect<ITableXtCompany>.Create("DAL_table_xt_company", "Willson.JsbManager.Dal");
            xt_company_DAL.SetSortType(false);
            xt_company_sb_info_DAL = Reflect<ITableXtCompany_sb_info>.Create("DAL_table_xt_company_sb_info", "Willson.JsbManager.Dal");
            xt_sb_month_DAL = Reflect<ITableXtSbMonth>.Create("DAL_table_xt_sb_month", "Willson.JsbManager.Dal");
        }

        /// <summary>
        /// 保存录入信息
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <param name="sb_id">水表ID</param>
        /// <param name="id">月信息表ID</param>
        /// <param name="hb">换表度数</param>
        /// <param name="cjs">抄机数</param>
        /// <param name="ysl">用水量</param>
        /// <param name="memo">备注信息</param>
        public void Save(int year,int month,int sb_id,int id, int hb, int cjs, int ysl, string memo)
        {
            Hashtable hash = new Hashtable();
            int m_year = 0;
            int m_month = 0;


            hash.Add("sb_hb",hb);
            hash.Add("sb_cjs",cjs);
            hash.Add("sb_ysl",ysl);
            hash.Add("memo",memo);

            xt_sb_month_DAL.Update(id, hash);

            if (month == 12)
            {
                m_year = year + 1;
                m_month = 1;
            }
            else
            {
                m_year = year;
                m_month = month + 1;
            }

            Table_xt_sb_month_entity info = new Table_xt_sb_month_entity();
            info.Sb_id = sb_id;
            info.Sb_year = m_year;
            info.Sb_month = m_month;
            info.Sb_begin = cjs;

            xt_sb_month_DAL.Insert(info);
            
        }

        /// <summary>
        /// 获取录入列表
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <returns></returns>
        public BindingList<BESbMonthInput> GetListInfo(int year, int month)
        {
            BindingList<BESbMonthInput> list = new BindingList<BESbMonthInput>();

            IEnumerator<TableXtCompanyEntity> company_ator = xt_company_DAL.GetAll(true).GetEnumerator();

            while (company_ator.MoveNext())
            {
                if (company_ator.Current.Remove == 0)  //过滤掉已经移除的记录
                {
                    Hashtable hash = new Hashtable();
                    hash.Add("company_id",company_ator.Current.Id);

                    IEnumerator<TableXtCompanySbInfoEntity> sb_ator = xt_company_sb_info_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

                    while (sb_ator.MoveNext())
                    {
                        if (sb_ator.Current.Remove == 0) //过滤掉已经移除的记录
                        {
                            Hashtable hash2 = new Hashtable();
                            hash2.Add("sb_year",year);
                            hash2.Add("sb_month",month);
                            hash2.Add("sb_id",sb_ator.Current.Id);
                            hash2.Add("sb_cjs", 0);
                            hash2.Add("sb_hb", 0);

                            Table_xt_sb_month_entity  entity = xt_sb_month_DAL.Find(hash2);

                            if (entity == null) continue;

                            BESbMonthInput info = new BESbMonthInput();

                            info.Sb_month_id = entity.Id;
                            info.User_code = sb_ator.Current.User_code;
                            info.Ht_code = sb_ator.Current.Ht_code;
                            info.Company_name = company_ator.Current.Name;
                            info.Company_address = company_ator.Current.Address;
                            info.Sb_begin = entity.Sb_begin;
                            info.Sb_id = sb_ator.Current.Id;

                            list.Add(info);
                            
                        }
                    }
                }
            }

            return list;
        }
    }
}
